<!DOCTYPE html>

<html
  xmlns="http://www.w3.org/1999/xhtml"
  >
  <head>
    <title>Hexbin map &#8211; from Data to Viz</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="generator" content="pandoc" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <meta
      name="description"
      content="An extensive description of Hexbin
map. Definition, examples, input data, common caveats, tool to build it and potential alternatives."
    />
    <meta
      name="keywords"
      content="Data,Dataviz,Datavisualization,Plot,Chart,Graph,R,Python,D3,Learning,Caveat,Pitfall,Mistake,Classification"
    />
    <meta name="author" content="Yan Holtz and Conor Healy" />
    <link rel="icon" href="img/logo/data-to-viz.ico" />

    <!-- Control appearance when share by social media -->
    <meta property="og:title" content="Hexbin map" />
    <meta
      property="og:image"
      content="https://github.com/holtzy/data_to_viz/raw/master/img/section/MapHexbinSmall.png"
    />
    <meta
      property="og:description"
      content="An extensive description of Hexbin
map. Definition, examples, input data, common caveats, tool to build it and potential alternatives."
    />
    <meta
      property="og:url"
      content="www.data-to-viz.com/caveat/hexbinmap.html"
    />
    <meta property="og:type" content="website" />

    <style>
      .myheader {
        background-image: url("https://github.com/holtzy/data_to_viz/raw/master/img/overview_RGG_faded.png");
        min-height: 300px;
        background-size: 100% 100%;

        /* Create the parallax scrolling effect */
        background-attachment: fixed;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;

        height: 100%;
        width: 100% !important;
      }
      .mytitle {
        text-transform: uppercase;
        font-family: "Montserrat";
        font-size: 200%;
        font-weight: 500;
        letter-spacing: 4px;
      }
    </style>

    <div class="myheader" style="text-align: center">
      <br />
      <a href="http://www.data-to-viz.com"
        ><img
          align="left"
          src="https://github.com/holtzy/data_to_viz/raw/master/img/logo/typo.png"
          style="width: 150px; vertical-align: top; margin-left: 5%"
      /></a>
      <br /><br />
      <br /><br />
      <a href="http://www.data-to-viz.com">
        <img
          src="https://github.com/holtzy/data_to_viz/raw/master/img/section/MapHexbinSmall.png"
      /></a>
      <br /><br />
      <p class="mytitle">Hexbin map</p>
      <p style="color: #d0d0d0">
        <a href="#definition">definition</a> - <a href="#mistake">mistake</a> -
        <a href="#related">related</a> -
        <a href="#code">code</a>
      </p>
    </div>

         <script src="libs/header-attrs-2.27/header-attrs.js"></script>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="libs/bootstrap-3.3.5/css/lumen.min.css" rel="stylesheet" />
<script src="libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
       h1.title {font-size: 38px;}
       h2 {font-size: 30px;}
       h3 {font-size: 24px;}
       h4 {font-size: 18px;}
       h5 {font-size: 16px;}
       h6 {font-size: 12px;}
       code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
       pre:not([class]) { background-color: white }</style>
<script src="libs/navigation-1.1/tabsets.js"></script>
<script src="libs/navigation-1.1/codefolding.js"></script>
<link href="libs/pagedtable-1.1/css/pagedtable.css" rel="stylesheet" />
<script src="libs/pagedtable-1.1/js/pagedtable.js"></script>       <style type="text/css">
      code {
        white-space: pre;
      }
    </style>
    <style type="text/css">
      pre > code.sourceCode { white-space: pre; position: relative; }
      pre > code.sourceCode > span { line-height: 1.25; }
      pre > code.sourceCode > span:empty { height: 1.2em; }
      .sourceCode { overflow: visible; }
      code.sourceCode > span { color: inherit; text-decoration: inherit; }
      div.sourceCode { margin: 1em 0; }
      pre.sourceCode { margin: 0; }
      @media screen {
      div.sourceCode { overflow: auto; }
      }
      @media print {
      pre > code.sourceCode { white-space: pre-wrap; }
      pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
      }
      pre.numberSource code
        { counter-reset: source-line 0; }
      pre.numberSource code > span
        { position: relative; left: -4em; counter-increment: source-line; }
      pre.numberSource code > span > a:first-child::before
        { content: counter(source-line);
          position: relative; left: -1em; text-align: right; vertical-align: baseline;
          border: none; display: inline-block;
          -webkit-touch-callout: none; -webkit-user-select: none;
          -khtml-user-select: none; -moz-user-select: none;
          -ms-user-select: none; user-select: none;
          padding: 0 4px; width: 4em;
          color: #aaaaaa;
        }
      pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
      div.sourceCode
        {   }
      @media screen {
      pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
      }
      code span.al { color: #ff0000; font-weight: bold; } /* Alert */
      code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
      code span.at { color: #7d9029; } /* Attribute */
      code span.bn { color: #40a070; } /* BaseN */
      code span.bu { color: #008000; } /* BuiltIn */
      code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
      code span.ch { color: #4070a0; } /* Char */
      code span.cn { color: #880000; } /* Constant */
      code span.co { color: #60a0b0; font-style: italic; } /* Comment */
      code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
      code span.do { color: #ba2121; font-style: italic; } /* Documentation */
      code span.dt { color: #902000; } /* DataType */
      code span.dv { color: #40a070; } /* DecVal */
      code span.er { color: #ff0000; font-weight: bold; } /* Error */
      code span.ex { } /* Extension */
      code span.fl { color: #40a070; } /* Float */
      code span.fu { color: #06287e; } /* Function */
      code span.im { color: #008000; font-weight: bold; } /* Import */
      code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
      code span.kw { color: #007020; font-weight: bold; } /* Keyword */
      code span.op { color: #666666; } /* Operator */
      code span.ot { color: #007020; } /* Other */
      code span.pp { color: #bc7a00; } /* Preprocessor */
      code span.sc { color: #4070a0; } /* SpecialChar */
      code span.ss { color: #bb6688; } /* SpecialString */
      code span.st { color: #4070a0; } /* String */
      code span.va { color: #19177c; } /* Variable */
      code span.vs { color: #4070a0; } /* VerbatimString */
      code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
            div.sourceCode {
        overflow-x: visible;
      }
          </style>
        <style type="text/css">
      pre:not([class]) {
        background-color: white;
      }
    </style>
           <style type="text/css">
      h1 {
        font-size: 34px;
      }
      h1.title {
        font-size: 38px;
      }
      h2 {
        font-size: 30px;
      }
      h3 {
        font-size: 24px;
      }
      h4 {
        font-size: 18px;
      }
      h5 {
        font-size: 16px;
      }
      h6 {
        font-size: 12px;
      }
      .table th:not([align]) {
        text-align: left;
      }
          </style>
         <link
      rel="stylesheet"
      href="style.css"
      type="text/css"
      
    />
      </head>

  <body>
        <style type="text/css">
      .main-container {
        max-width: 940px;
        margin-left: auto;
        margin-right: auto;
      }
      code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
      }
      img {
        max-width: 100%;
        height: auto;
      }
      .tabbed-pane {
        padding-top: 12px;
      }
      button.code-folding-btn:focus {
        outline: none;
      }
    </style>

     
    <div class="container-fluid main-container">
      <!-- tabsets -->
      <script>
        $(document).ready(function () {
          window.buildTabsets("TOC");
        });
      </script>

      <!-- code folding -->
            <style type="text/css">
        .code-folding-btn {
          margin-bottom: 4px;
        }
      </style>
      <script>
        $(document).ready(function () {
                          window.initializeCodeFolding("hide" === "show");
                });
      </script>
         
                    <div class="fluid-row" id="header">
                        <div class="btn-group pull-right">
              <button
                type="button"
                class="btn btn-default btn-xs dropdown-toggle"
                data-toggle="dropdown"
                aria-haspopup="true"
                aria-expanded="false"
              >
                <span>Show Code</span> <span class="caret"></span>
              </button>
              <ul class="dropdown-menu" style="min-width: 50px">
                                <li><a id="rmd-show-all-code" href="#">Show All Code</a></li>
                <li><a id="rmd-hide-all-code" href="#">Hide All Code</a></li>
                                </ul>
            </div>

                         </div>

              <div id="definition" class="section level1 mycontent">
<h1>Definition</h1>
<hr />
<p>The term <code>hexbin map</code> refers to two different
concepts:</p>
<ul>
<li>The first definition is based on an unusual geospatial object where
all regions of the map are represented as hexagons. Here is an example
representing the US states, each represented as an hexagon. Note that
here the data input provides <code>one value per state</code> and a
<code>specific shape file</code> with hexagone boundaries.</li>
</ul>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="co"># library</span></span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="fu">library</span>(sf)</span>
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="fu">library</span>(RColorBrewer)</span>
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a></span>
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="co"># Download the Hexagon boundaries at geojson format here: https://team.carto.com/u/andrew/tables/andrew.us_states_hexgrid/public/map.</span></span>
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="co"># Load this file. (Note: I stored in a folder called DATA)</span></span>
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a>my_sf <span class="ot">&lt;-</span> <span class="fu">read_sf</span>(<span class="st">&quot;us_states_hexgrid.geojson.json&quot;</span>)</span>
<span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a></span>
<span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a><span class="co"># Bit of reformatting</span></span>
<span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a>my_sf <span class="ot">&lt;-</span> my_sf <span class="sc">%&gt;%</span></span>
<span id="cb1-13"><a href="#cb1-13" tabindex="-1"></a>  <span class="fu">mutate</span>(<span class="at">google_name =</span> <span class="fu">gsub</span>(<span class="st">&quot; </span><span class="sc">\\</span><span class="st">(United States</span><span class="sc">\\</span><span class="st">)&quot;</span>, <span class="st">&quot;&quot;</span>, google_name))</span>
<span id="cb1-14"><a href="#cb1-14" tabindex="-1"></a></span>
<span id="cb1-15"><a href="#cb1-15" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">read.table</span>(<span class="st">&quot;https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/State_mariage_rate.csv&quot;</span>,</span>
<span id="cb1-16"><a href="#cb1-16" tabindex="-1"></a>  <span class="at">header =</span> T, <span class="at">sep =</span> <span class="st">&quot;,&quot;</span>, <span class="at">na.strings =</span> <span class="st">&quot;---&quot;</span></span>
<span id="cb1-17"><a href="#cb1-17" tabindex="-1"></a>)</span>
<span id="cb1-18"><a href="#cb1-18" tabindex="-1"></a></span>
<span id="cb1-19"><a href="#cb1-19" tabindex="-1"></a><span class="co"># Show it</span></span>
<span id="cb1-20"><a href="#cb1-20" tabindex="-1"></a>my_sf_wed <span class="ot">&lt;-</span> my_sf <span class="sc">%&gt;%</span></span>
<span id="cb1-21"><a href="#cb1-21" tabindex="-1"></a>  <span class="fu">left_join</span>(data, <span class="at">by =</span> <span class="fu">c</span>(<span class="st">&quot;google_name&quot;</span> <span class="ot">=</span> <span class="st">&quot;state&quot;</span>))</span>
<span id="cb1-22"><a href="#cb1-22" tabindex="-1"></a></span>
<span id="cb1-23"><a href="#cb1-23" tabindex="-1"></a><span class="co"># Prepare binning</span></span>
<span id="cb1-24"><a href="#cb1-24" tabindex="-1"></a>my_sf_wed<span class="sc">$</span>bin <span class="ot">&lt;-</span> <span class="fu">cut</span>(my_sf_wed<span class="sc">$</span>y_2015,</span>
<span id="cb1-25"><a href="#cb1-25" tabindex="-1"></a>  <span class="at">breaks =</span> <span class="fu">c</span>(<span class="fu">seq</span>(<span class="dv">5</span>, <span class="dv">10</span>), <span class="cn">Inf</span>),</span>
<span id="cb1-26"><a href="#cb1-26" tabindex="-1"></a>  <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">&quot;5-6&quot;</span>, <span class="st">&quot;6-7&quot;</span>, <span class="st">&quot;7-8&quot;</span>, <span class="st">&quot;8-9&quot;</span>, <span class="st">&quot;9-10&quot;</span>, <span class="st">&quot;10+&quot;</span>),</span>
<span id="cb1-27"><a href="#cb1-27" tabindex="-1"></a>  <span class="at">include.lowest =</span> <span class="cn">TRUE</span></span>
<span id="cb1-28"><a href="#cb1-28" tabindex="-1"></a>)</span>
<span id="cb1-29"><a href="#cb1-29" tabindex="-1"></a></span>
<span id="cb1-30"><a href="#cb1-30" tabindex="-1"></a><span class="co"># Prepare a color scale coming from the viridis color palette</span></span>
<span id="cb1-31"><a href="#cb1-31" tabindex="-1"></a><span class="fu">library</span>(viridis)</span>
<span id="cb1-32"><a href="#cb1-32" tabindex="-1"></a>my_palette <span class="ot">&lt;-</span> <span class="fu">rev</span>(<span class="fu">magma</span>(<span class="dv">8</span>))[<span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>, <span class="sc">-</span><span class="dv">8</span>)]</span>
<span id="cb1-33"><a href="#cb1-33" tabindex="-1"></a></span>
<span id="cb1-34"><a href="#cb1-34" tabindex="-1"></a><span class="co"># plot</span></span>
<span id="cb1-35"><a href="#cb1-35" tabindex="-1"></a><span class="fu">ggplot</span>(my_sf_wed) <span class="sc">+</span></span>
<span id="cb1-36"><a href="#cb1-36" tabindex="-1"></a>  <span class="fu">geom_sf</span>(<span class="fu">aes</span>(<span class="at">fill =</span> bin), <span class="at">linewidth =</span> <span class="dv">0</span>, <span class="at">alpha =</span> <span class="fl">0.9</span>) <span class="sc">+</span></span>
<span id="cb1-37"><a href="#cb1-37" tabindex="-1"></a>  <span class="fu">geom_sf_text</span>(<span class="fu">aes</span>(<span class="at">label =</span> iso3166_2), <span class="at">color =</span> <span class="st">&quot;white&quot;</span>, <span class="at">size =</span> <span class="dv">3</span>, <span class="at">alpha =</span> <span class="fl">0.6</span>) <span class="sc">+</span></span>
<span id="cb1-38"><a href="#cb1-38" tabindex="-1"></a>  <span class="fu">theme_void</span>() <span class="sc">+</span></span>
<span id="cb1-39"><a href="#cb1-39" tabindex="-1"></a>  <span class="fu">scale_fill_manual</span>(</span>
<span id="cb1-40"><a href="#cb1-40" tabindex="-1"></a>    <span class="at">values =</span> my_palette,</span>
<span id="cb1-41"><a href="#cb1-41" tabindex="-1"></a>    <span class="at">name =</span> <span class="st">&quot;Wedding per 1000 people in 2015&quot;</span>,</span>
<span id="cb1-42"><a href="#cb1-42" tabindex="-1"></a>    <span class="at">guide =</span> <span class="fu">guide_legend</span>(</span>
<span id="cb1-43"><a href="#cb1-43" tabindex="-1"></a>      <span class="at">keyheight =</span> <span class="fu">unit</span>(<span class="dv">3</span>, <span class="at">units =</span> <span class="st">&quot;mm&quot;</span>),</span>
<span id="cb1-44"><a href="#cb1-44" tabindex="-1"></a>      <span class="at">keywidth =</span> <span class="fu">unit</span>(<span class="dv">12</span>, <span class="at">units =</span> <span class="st">&quot;mm&quot;</span>),</span>
<span id="cb1-45"><a href="#cb1-45" tabindex="-1"></a>      <span class="at">label.position =</span> <span class="st">&quot;bottom&quot;</span>, <span class="at">title.position =</span> <span class="st">&quot;top&quot;</span>, <span class="at">nrow =</span> <span class="dv">1</span></span>
<span id="cb1-46"><a href="#cb1-46" tabindex="-1"></a>    )</span>
<span id="cb1-47"><a href="#cb1-47" tabindex="-1"></a>  ) <span class="sc">+</span></span>
<span id="cb1-48"><a href="#cb1-48" tabindex="-1"></a>  <span class="fu">ggtitle</span>(<span class="st">&quot;A map of marriage rates, state by state&quot;</span>) <span class="sc">+</span></span>
<span id="cb1-49"><a href="#cb1-49" tabindex="-1"></a>  <span class="fu">theme</span>(</span>
<span id="cb1-50"><a href="#cb1-50" tabindex="-1"></a>    <span class="at">legend.position =</span> <span class="fu">c</span>(<span class="fl">0.5</span>, <span class="fl">0.9</span>),</span>
<span id="cb1-51"><a href="#cb1-51" tabindex="-1"></a>    <span class="at">text =</span> <span class="fu">element_text</span>(<span class="at">color =</span> <span class="st">&quot;#22211d&quot;</span>),</span>
<span id="cb1-52"><a href="#cb1-52" tabindex="-1"></a>    <span class="at">plot.background =</span> <span class="fu">element_rect</span>(<span class="at">fill =</span> <span class="st">&quot;#f5f5f2&quot;</span>, <span class="at">color =</span> <span class="cn">NA</span>),</span>
<span id="cb1-53"><a href="#cb1-53" tabindex="-1"></a>    <span class="at">panel.background =</span> <span class="fu">element_rect</span>(<span class="at">fill =</span> <span class="st">&quot;#f5f5f2&quot;</span>, <span class="at">color =</span> <span class="cn">NA</span>),</span>
<span id="cb1-54"><a href="#cb1-54" tabindex="-1"></a>    <span class="at">legend.background =</span> <span class="fu">element_rect</span>(<span class="at">fill =</span> <span class="st">&quot;#f5f5f2&quot;</span>, <span class="at">color =</span> <span class="cn">NA</span>),</span>
<span id="cb1-55"><a href="#cb1-55" tabindex="-1"></a>    <span class="at">plot.title =</span> <span class="fu">element_text</span>(</span>
<span id="cb1-56"><a href="#cb1-56" tabindex="-1"></a>      <span class="at">size =</span> <span class="dv">22</span>, <span class="at">hjust =</span> <span class="fl">0.5</span>, <span class="at">color =</span> <span class="st">&quot;#4e4d47&quot;</span>,</span>
<span id="cb1-57"><a href="#cb1-57" tabindex="-1"></a>      <span class="at">margin =</span> <span class="fu">margin</span>(<span class="at">b =</span> <span class="sc">-</span><span class="fl">0.1</span>, <span class="at">t =</span> <span class="fl">0.4</span>, <span class="at">l =</span> <span class="dv">2</span>, <span class="at">unit =</span> <span class="st">&quot;cm&quot;</span>)</span>
<span id="cb1-58"><a href="#cb1-58" tabindex="-1"></a>    )</span>
<span id="cb1-59"><a href="#cb1-59" tabindex="-1"></a>  )</span></code></pre></div>
<p><img src="hexbinmap_files/figure-html/unnamed-chunk-1-1.png" width="768" style="display: block; margin: auto;" /></p>
<p><br></p>
<ul>
<li>The second definition refers to the utilisation of <a
href="https://www.data-to-viz.com/graph/density2d.html">2D density</a>
techniques. Think of a scatterplot where the X axis is the longitude and
the Y axis is the latitude. The graphic area is divided in a multitude
of hexagones and the number of data point in each is counted and
represented using a color gradient. Note that here
<code>no map boundaries is needed</code>. It requires only a
<code>list of latitude and longitude</code>.</li>
</ul>
</div>
<div class="row"
style="padding-left: 50px; padding-right: 50px; margin-left: auto; margin-right: auto; background-color: #f5f5f2;">
<p><br></p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="co"># Libraries</span></span>
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="fu">library</span>(viridis)</span>
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="fu">library</span>(hrbrthemes)</span>
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a><span class="fu">library</span>(kableExtra)</span>
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="fu">options</span>(<span class="at">knitr.table.format =</span> <span class="st">&quot;html&quot;</span>)</span>
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a><span class="fu">library</span>(mapdata)</span>
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a></span>
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a><span class="co"># Load dataset from github</span></span>
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">read.table</span>(<span class="st">&quot;https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/17_ListGPSCoordinates.csv&quot;</span>, <span class="at">sep=</span><span class="st">&quot;,&quot;</span>, <span class="at">header=</span>T)</span>
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a></span>
<span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a><span class="co"># plot</span></span>
<span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a>data <span class="sc">%&gt;%</span></span>
<span id="cb2-14"><a href="#cb2-14" tabindex="-1"></a>  <span class="fu">filter</span>(homecontinent<span class="sc">==</span><span class="st">&#39;Europe&#39;</span>) <span class="sc">%&gt;%</span></span>
<span id="cb2-15"><a href="#cb2-15" tabindex="-1"></a>  <span class="fu">ggplot</span>( <span class="fu">aes</span>(<span class="at">x=</span>homelon, <span class="at">y=</span>homelat)) <span class="sc">+</span></span>
<span id="cb2-16"><a href="#cb2-16" tabindex="-1"></a>    <span class="fu">geom_hex</span>(<span class="at">bins=</span><span class="dv">59</span>) <span class="sc">+</span></span>
<span id="cb2-17"><a href="#cb2-17" tabindex="-1"></a>    ggplot2<span class="sc">::</span><span class="fu">annotate</span>(<span class="st">&quot;text&quot;</span>, <span class="at">x =</span> <span class="sc">-</span><span class="dv">27</span>, <span class="at">y =</span> <span class="dv">72</span>, <span class="at">label=</span><span class="st">&quot;Where people tweet about #Surf&quot;</span>, <span class="at">colour =</span> <span class="st">&quot;black&quot;</span>, <span class="at">size=</span><span class="dv">5</span>, <span class="at">alpha=</span><span class="dv">1</span>, <span class="at">hjust=</span><span class="dv">0</span>) <span class="sc">+</span></span>
<span id="cb2-18"><a href="#cb2-18" tabindex="-1"></a>    ggplot2<span class="sc">::</span><span class="fu">annotate</span>(<span class="st">&quot;segment&quot;</span>, <span class="at">x =</span> <span class="sc">-</span><span class="dv">27</span>, <span class="at">xend =</span> <span class="dv">10</span>, <span class="at">y =</span> <span class="dv">70</span>, <span class="at">yend =</span> <span class="dv">70</span>, <span class="at">colour =</span> <span class="st">&quot;black&quot;</span>, <span class="at">size=</span><span class="fl">0.2</span>, <span class="at">alpha=</span><span class="dv">1</span>) <span class="sc">+</span></span>
<span id="cb2-19"><a href="#cb2-19" tabindex="-1"></a>    <span class="fu">theme_void</span>() <span class="sc">+</span></span>
<span id="cb2-20"><a href="#cb2-20" tabindex="-1"></a>    <span class="fu">xlim</span>(<span class="sc">-</span><span class="dv">30</span>, <span class="dv">70</span>) <span class="sc">+</span></span>
<span id="cb2-21"><a href="#cb2-21" tabindex="-1"></a>    <span class="fu">ylim</span>(<span class="dv">24</span>, <span class="dv">72</span>) <span class="sc">+</span></span>
<span id="cb2-22"><a href="#cb2-22" tabindex="-1"></a>    <span class="fu">scale_fill_viridis</span>(</span>
<span id="cb2-23"><a href="#cb2-23" tabindex="-1"></a>      <span class="at">trans =</span> <span class="st">&quot;log&quot;</span>,</span>
<span id="cb2-24"><a href="#cb2-24" tabindex="-1"></a>      <span class="at">breaks =</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">7</span>,<span class="dv">54</span>,<span class="dv">403</span>,<span class="dv">3000</span>),</span>
<span id="cb2-25"><a href="#cb2-25" tabindex="-1"></a>      <span class="at">name=</span><span class="st">&quot;Tweet # recorded in 8 months&quot;</span>,</span>
<span id="cb2-26"><a href="#cb2-26" tabindex="-1"></a>      <span class="at">guide =</span> <span class="fu">guide_legend</span>( <span class="at">keyheight =</span> <span class="fu">unit</span>(<span class="fl">2.5</span>, <span class="at">units =</span> <span class="st">&quot;mm&quot;</span>), <span class="at">keywidth=</span><span class="fu">unit</span>(<span class="dv">10</span>, <span class="at">units =</span> <span class="st">&quot;mm&quot;</span>), <span class="at">label.position =</span> <span class="st">&quot;bottom&quot;</span>, <span class="at">title.position =</span> <span class="st">&#39;top&#39;</span>, <span class="at">nrow=</span><span class="dv">1</span>)</span>
<span id="cb2-27"><a href="#cb2-27" tabindex="-1"></a>    )  <span class="sc">+</span></span>
<span id="cb2-28"><a href="#cb2-28" tabindex="-1"></a>    <span class="fu">ggtitle</span>( <span class="st">&quot;&quot;</span> ) <span class="sc">+</span></span>
<span id="cb2-29"><a href="#cb2-29" tabindex="-1"></a>    <span class="fu">theme</span>(</span>
<span id="cb2-30"><a href="#cb2-30" tabindex="-1"></a>      <span class="at">legend.position =</span> <span class="fu">c</span>(<span class="fl">0.8</span>, <span class="fl">0.09</span>),</span>
<span id="cb2-31"><a href="#cb2-31" tabindex="-1"></a>      <span class="at">legend.title=</span><span class="fu">element_text</span>(<span class="at">color=</span><span class="st">&quot;black&quot;</span>, <span class="at">size=</span><span class="dv">8</span>),</span>
<span id="cb2-32"><a href="#cb2-32" tabindex="-1"></a>      <span class="at">text =</span> <span class="fu">element_text</span>(<span class="at">color =</span> <span class="st">&quot;#22211d&quot;</span>),</span>
<span id="cb2-33"><a href="#cb2-33" tabindex="-1"></a>      <span class="at">plot.background =</span> <span class="fu">element_rect</span>(<span class="at">fill =</span> <span class="st">&quot;#f5f5f2&quot;</span>, <span class="at">color =</span> <span class="cn">NA</span>),</span>
<span id="cb2-34"><a href="#cb2-34" tabindex="-1"></a>      <span class="at">panel.background =</span> <span class="fu">element_rect</span>(<span class="at">fill =</span> <span class="st">&quot;#f5f5f2&quot;</span>, <span class="at">color =</span> <span class="cn">NA</span>),</span>
<span id="cb2-35"><a href="#cb2-35" tabindex="-1"></a>      <span class="at">legend.background =</span> <span class="fu">element_rect</span>(<span class="at">fill =</span> <span class="st">&quot;#f5f5f2&quot;</span>, <span class="at">color =</span> <span class="cn">NA</span>),</span>
<span id="cb2-36"><a href="#cb2-36" tabindex="-1"></a>      <span class="at">plot.title =</span> <span class="fu">element_text</span>(<span class="at">size=</span> <span class="dv">13</span>, <span class="at">hjust=</span><span class="fl">0.1</span>, <span class="at">color =</span> <span class="st">&quot;#4e4d47&quot;</span>, <span class="at">margin =</span> <span class="fu">margin</span>(<span class="at">b =</span> <span class="sc">-</span><span class="fl">0.1</span>, <span class="at">t =</span> <span class="fl">0.4</span>, <span class="at">l =</span> <span class="dv">2</span>, <span class="at">unit =</span> <span class="st">&quot;cm&quot;</span>)),</span>
<span id="cb2-37"><a href="#cb2-37" tabindex="-1"></a>    )</span></code></pre></div>
<p><img src="hexbinmap_files/figure-html/unnamed-chunk-2-1.png" width="864" style="display: block; margin: auto;" /></p>
<p><br></p>
</div>
<div class="mycontent">
<p><br> <em>Note on the first map</em>: You can learn more about this
story <a
href="https://www.data-to-viz.com/story/RegionWithValue.html">here</a>.
Data comes from <a
href="https://team.carto.com/u/andrew/tables/andrew.us_states_hexgrid/public/map">here</a>.
Code has been inspired from <a
href="https://timogrossenbacher.ch/2016/12/beautiful-thematic-maps-with-ggplot2-only/">this
post</a> and <a
href="https://rud.is/b/2015/05/15/u-s-drought-monitoring-with-hexbin-state-maps-in-r/">that
one</a>.</p>
<p><em>Note on the second map</em>: This map shows the geographic
position of about 200k tweets containing the hashtags
<code>#surf</code>, <code>#windsurf</code> or <code>#kitesurf</code>.
Read more about it <a
href="https://www.data-to-viz.com/story/GPSCoordWithoutValue.html">here</a>.</p>
<div id="what-for" class="section level1">
<h1>What for</h1>
<hr />
<p>Hexbin or grid map has an <code>advantage</code> over usual <a
href="https://www.data-to-viz.com/graph/choropleth.html">choropleth
maps</a>. In choropleths, a large polygon’s data looks more emphasized
just because of its size, what introduces a bias. Here with hexbin, each
region is represented equally dismissing the bias.</p>
<p>There’s a <code>drawback</code> to this format though. Map readers
generally recognize a geographic area by it’s shape and orientation to
other areas. For instance, the geography of the US is well known and
people easily identify different regions. In hexbin maps, these
landmarks do not exist anymore what can confuse the audience. One
solution for this is to choose a basemap that uses labels on top of your
data layer.</p>
</div>
<div id="variation" class="section level1">
<h1>Variation</h1>
<hr />
<p>Hexbin map uses hexagons to split the area in several parts and
attribute a color to it. Note that it is possible to use square instead
of hexagones, resulting in a <a
href="https://www.data-to-viz.com/graph/density2d.html">2D histogram
map</a>.</p>
</div>
<div id="mistake" class="section level1">
<h1>Common mistakes</h1>
<hr />
<ul>
<li>Normalize your variable: you cannot compare raw numbers between
regions of distinct size or population.</li>
<li>Take a huge care when choosing the continuous color palette.</li>
<li>Don’t forget the legend.</li>
</ul>
</div>
<div id="related" class="section level1">
<h1>Related</h1>
<hr />
<div class="row">
<div class="col-lg-3 col-md-6 col-sm-6">
<a href="https://www.data-to-viz.com/graph/choropleth.html" class="btn btn-primary mybtnrelated" style="margin-bottom:4px;white-space: normal !important;">
<img  src="https://github.com/holtzy/data_to_viz/raw/master/img/section/ChoroplethSmall.png">
<p class="mytitlerelated">
Choropleth
</p>
<p class="mytextrelated">
Display a aggregated value for each region of the map using color
gradients.
</p>
</a>
</div>
<div class="col-lg-3 col-md-6 col-sm-6">
<a href="https://www.data-to-viz.com/graph/cartogram.html" class="btn btn-primary mybtnrelated" style="margin-bottom:4px;white-space: normal !important;">
<img  src="https://github.com/holtzy/data_to_viz/raw/master/img/section/CartogramSmall.png">
<p class="mytitlerelated">
Cartogram
</p>
<p class="mytextrelated">
A circular version of a Treemap to visualize a hierarchical organization
</p>
</a>
</div>
<div class="col-lg-3 col-md-6 col-sm-6">
<a href="https://www.data-to-viz.com/graph/bubblemap.html" class="btn btn-primary mybtnrelated" style="margin-bottom:4px;white-space: normal !important;">
<img  src="https://github.com/holtzy/data_to_viz/raw/master/img/section/BubbleMapSmall.png">
<p class="mytitlerelated">
Bubble map
</p>
<p class="mytextrelated">
Display bubble on a map background to represent the value of a numeric
variable.
</p>
</a>
</div>
<div class="col-lg-3 col-md-6 col-sm-6">
<a href="https://www.data-to-viz.com/graph/map.html" class="btn btn-primary mybtnrelated" style="margin-bottom:4px;white-space: normal !important;">
<img  src="https://github.com/holtzy/data_to_viz/raw/master/img/section/MapSmall.png">
<p class="mytitlerelated">
Background Map
</p>
<p class="mytextrelated">
The basis for any geographic data visualization is to build a good
background map.
</p>
</a>
</div>
</div>
</div>
<div id="code" class="section level1">
<h1>Build your own</h1>
<hr />
<p>The <a href="https://r-graph-gallery.com/hexbin-map.html">R</a>, <a
href="https:/python-graph-gallery.com/hexbin-map/">Python</a>, <a
href="https://www.react-graph-gallery.com/hexbin-map">React</a> and <a
href="https://d3-graph-gallery.com/hexbinmap.html">D3</a> graph
galleries are 4 websites providing hundreds of chart example, always
providing the reproducible code. Click the button below to see how to
build the chart you need with your favorite programing language.</p>
<p>
<a href="https://r-graph-gallery.com/hexbin-map.html" class="btn btn-primary">R
graph gallery</a>
<a href="https://python-graph-gallery.com/hexbin-map/" class="btn btn-primary">Python
gallery</a>
<a href="https://www.react-graph-gallery.com/hexbin-map" class="btn btn-primary">React
gallery</a>
<a href="https://d3-graph-gallery.com/hexbinmap.html" class="btn btn-primary">D3
gallery</a>
</p>
</div>
</div>

          <section
            id="form"
            style="
              margin-top: 70px;
              padding-bottom: 70px;
              padding-top: 70px;
              background-color: #f8f9fa;
            "
          >
            <div class="container">
              <div
                style="
                  display: flex;
                  flex-direction: column;
                  align-items: center;
                "
              >
                <h2 style="font-size: 44px" class="text-center">
                  Dataviz decision tree
                </h2>
                <p
                  class="text-center"
                  style="max-width: 600px; font-size: 18px"
                >
                  <a href="https://www.data-to-viz.com">Data To Viz</a> is a
                  comprehensive <b>classification of chart types</b> organized
                  by data input format. Get a high-resolution version of our
                  decision tree delivered to your inbox now!
                </p>
                <div
                  style="
                    margin-top: -50px;
                    width: 100%;
                    display: flex;
                    justify-content: center;
                  "
                >
                  <script
                    async
                    data-uid="8010f15fd2"
                    src="https://prodigious-trailblazer-3628.ck.page/8010f15fd2/index.js"
                  ></script>
                </div>
                <br />
                <div class="text-center">
                  <img
                    class="img-fluid"
                    src="../img/poster/poster_small.png"
                    alt="High Resolution Poster"
                    style="max-width: 400px"
                  />
                </div>
              </div>
            </div>
          </section>

           &nbsp;
<hr />
<p style="text-align: center;">A work by <a href="https://www.yan-holtz.com/">Yan Holtz</a> for <a href="https://data-to-viz.com">data-to-viz.com</a></p>

<!-- Add icon library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<!-- Add font awesome icons -->
<p style="text-align: center;">
	<a href="https://twitter.com/r_graph_gallery?lang=en" class="fa fa-twitter"></a>
	<a href="https://www.linkedin.com/in/yan-holtz-2477534a/" class="fa fa-linkedin"></a>
	<a href="https://github.com/holtzy/" class="fa fa-github"></a>
</p>

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-79254642-3"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-79254642-3');
</script>
<script>
var getOutboundLink = function(url) {
	gtag('event', 'click', {
		'event_category': 'outbound',
		'event_label': url,
		'transport_type': 'beacon',
		'event_callback': function(){document.location = url;}
	});
}
</script>

&nbsp;                </div>

    <script>

      // add bootstrap table styles to pandoc tables
      function bootstrapStylePandocTables() {
        $('tr.header').parent('thead').parent('table').addClass('table table-condensed');
      }
      $(document).ready(function () {
        bootstrapStylePandocTables();
      });

          </script>
       </body>
</html>
